package api

import (
	"github.com/smash-llj/Nexa-server/utils"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/smash-llj/Nexa-server/models/request"
	service "github.com/smash-llj/Nexa-server/service"
)

type FileApi struct {
	BaseApi
	Service *service.FiledService
}

func NewFileApi() FileApi {
	return FileApi{
		BaseApi: NewBaseApi(),
		Service: service.NewFileService(),
	}
}

// GetAllMessageFile 获取全部聊天文件 Api
func (f FileApi) GetAllMessageFile(C *gin.Context) {
	f.BaseApi.C = C
	UserId, err := strconv.Atoi(C.Param("userId"))
	if err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}

	MenageFileList, err := f.Service.GetAllMessageFile(UserId)
	if err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	C.JSON(http.StatusOK, gin.H{
		"data": MenageFileList,
	})

}

// SearchMessageFile 搜索最近聊天的文件 Api
func (f FileApi) SearchMessageFile(C *gin.Context) {
	var search request.SearchMessageFileReq
	if err := C.ShouldBind(&search); err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	MenageFileList, err := f.Service.SearchMessageFile(search)
	if err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	C.JSON(http.StatusOK, gin.H{
		"data": MenageFileList,
	})
}

// Upload 公共上传
func (f FileApi) Upload(C *gin.Context) {
	f.BaseApi.C = C
	baseUrl, err := utils.Upload(C, "file")
	if err != nil {
		f.Fail(Response{Msg: err.Error()})
		return
	}
	f.Success(Response{Msg: "上传成功", Data: baseUrl})
}
